var rewire = require("rewire")
const { generateUUID, currentMs } = rewire('../common/utils.cjs')
const { getPrisma } = rewire('../state.cjs')

const prisma = getPrisma()

async function updateUserInfo(data) {
    data['updatedAt'] = currentMs()
    const item = await prisma.instagram_user.findFirst({
        where: { uid: data.uid }
    })

    if (item) {
        return await prisma.instagram_user.update({
            where: { id: item.id },
            data: item
        })
    }
    else {
        const info = {id: generateUUID(), createdAt: currentMs(), ...data}
        return await prisma.instagram_user.create({
            data: info
        })
    }
}


async function setFollowState(uid, state) {
    return await prisma.instagram_user.updateMany({
        where: { uid: uid },
        data: {isFollow: state}
    })
}

module.exports = {updateUserInfo, setFollowState}
